﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using NetDon.Utilities;
using NetDon.eBoss.Data;
using NetDon.eBoss.Data.CollectionClasses;
using NetDon.eBoss.Data.EntityClasses;
using NetDon.eBoss.Data.HelperClasses;
using NetDon.eBoss.Portal.Common;
using NetDon.eBoss.Portal.HtmlTool;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace NetDon.OA.Portal.HtmlTool
{
    public class HtmlToolAccountService
    {
        #region HtmlAccount

        public int GetHtmlToolAccountCount(HtmlAccountCriteria criteria)
        {
            var collection = new HtmltoolaccountCollection();
            return
                int.Parse(
                    collection.GetScalar(HtmltoolaccountFieldIndex.Id, null, AggregateFunction.CountDistinct,
                                         GetSearchHtmlToolAccountPredicate(criteria)).ToString());
        }
        public HtmltoolaccountCollection GetHtmlToolAccounts()
        {
            var collection = new HtmltoolaccountCollection();
            collection.GetMulti(null);
            return collection;

        }

        public HtmltoolaccountEntity GetHtmlToolDefaultAccount()
        {
            var collection = new HtmltoolaccountCollection();
            collection.GetMulti(HtmltoolaccountFields.Isdefault == 1);
            return collection.FirstOrDefault();

        }

        public HtmltoolaccountCollection GetHtmlToolAccountsBySourceAccount(int sourceAccountId)
        {
            var collection = new HtmltoolaccountCollection();
            collection.GetMulti(HtmltoolaccountFields.From == sourceAccountId);
            return collection;

        }


        private IPredicate GetSearchHtmlToolAccountPredicate(HtmlAccountCriteria criteria)
        {
            return null;
        }

        public IEnumerable SearchHtmlToolAccountByCondition(HtmlAccountCriteria criteria, string sortExpression,
                                                            System.Web.UI.WebControls.SortDirection sortDirection,
                                                            int pageSize, int pageIndex)
        {
            var sort = GetHtmlToolAccountSortExpression(sortExpression, sortDirection);
            var collection = new HtmltoolaccountCollection();
            collection.GetMulti(GetSearchHtmlToolAccountPredicate(criteria), 0, sort,
                                null, pageIndex + 1, pageSize);




            return collection.Select(col => new
                                                {
                                                    Id = col.Id,
                                                    col.Accountname,
                                                    Sitecode =
                                                EnumUtilities.Parse<eBaySites>(col.Sitecode.ToString()).ToString(),
                                                    From =
                                                col.From.HasValue
                                                    ? new HtmltoolaccountEntity(col.From.Value).Accountname
                                                    : string.Empty,
                                                    Isdefault = col.Isdefault

                                                });
        }

        private SortExpression GetHtmlToolAccountSortExpression(string strInput, SortDirection sortDirection)
        {
            SortExpression sort;
            switch (strInput)
            {
                case "Accountname":
                    sort = new SortExpression(HtmltoolaccountFields.Accountname | (SortOperator)(int)sortDirection);
                    break;
                case "Sitecode":
                    sort = new SortExpression(HtmltoolaccountFields.Sitecode | (SortOperator)(int)sortDirection);
                    break;
                case "From":
                    sort = new SortExpression(HtmltoolaccountFields.From | (SortOperator)(int)sortDirection);
                    break;
                case "Isdefault":
                    sort = new SortExpression(HtmltoolaccountFields.Isdefault | (SortOperator)(int)sortDirection);
                    break;
                default:
                    sort = new SortExpression
                               {
                                   HtmltoolaccountFields.Isdefault | SortOperator.Descending
                               };
                    sort.Add(new SortClause(HtmltoolaccountFields.Accountname, SortOperator.Ascending));
                    break;

            }
            return sort;
        }

        #endregion

        #region HtmlAccountTemplate
        public HtmltoolaccounttemplateEntity GetHtmlToolAccountTemplate(int accountId, int categoryRuleId)
        {
            var collection = new HtmltoolaccounttemplateCollection();
            collection.GetMulti(HtmltoolaccounttemplateFields.Accountid == accountId & HtmltoolaccounttemplateFields.Categoryruleid ==categoryRuleId);
            return collection.FirstOrDefault();

        }
        public IEnumerable SearchHtmlToolAccountTemplate(int accountId)
        {

            var collection = new HtmltoolaccounttemplateCollection();
            var expression = new PredicateExpression();
            expression.Add(HtmltoolaccounttemplateFields.Accountid == accountId);
            collection.GetMulti(expression);

            var allCategories = new HtmltoolcategoryruleCollection();
            allCategories.GetMulti(null);
            foreach (var category in allCategories)
            {
                var accountTemplate =
                    collection.FirstOrDefault(template => template.Categoryruleid == category.Id);
                if (accountTemplate != null)
                {
                    yield return new
                                     {
                                         accountTemplate.Id,
                                         category.Category,
                                         CategoryId = category.Id,
                                         Content = accountTemplate.Content.HasValue ? accountTemplate.Content.ToString() : string.Empty,
                                         TemplateName = accountTemplate.Template == null ? string.Empty : accountTemplate.Template.Name
                                     };
                    continue;
                }
                yield return new
                {
                    Id = 0,
                    category.Category,
                    CategoryId = category.Id,
                    Content = string.Empty,
                    TemplateName = string.Empty
                };
            }


        }
        #endregion
    }

    [Serializable]
    public class HtmlAccountCriteria
    {

    }
}